﻿using System;
using Assets.Script.Data.Player;
using Assets.Script.HelpSystem.AtkSystem;
using Assets.Script.HelpSystem.AtkSystem.Base;
using Assets.Script.HelpSystem.Reward.Core;
using Assets.Script.Tools;
using Configs;
using LiteNetLib.Utils;
using Script.EnemySystem.Tools;
using Script.UiSystem;
using UnityEngine;

namespace Assets.Script.Human.Player
{
    public class LocalPlayerController : HumanInterface
    {
        public int ID { get; set; }
        public bool isMy = false;
        private PlayerBody playerBody;
        private PlayerActionManager actionManager;
        //[SerializeField] private UIFollowPlayer uIFollowPlayer; UI界面

        private PlayerState PlayerState;
        private AtkManager atkManager;
        private InputReaderMan readerMan;

        public void Init(PlayerBody playerBody, PlayerActionManager actionManager, AtkManager atkManager,bool isMy=false)
        {
            this.playerBody = playerBody;
            this.actionManager = actionManager;
            this.isMy = isMy;
            ID = playerBody.Id;
            PlayerState = playerBody.PlayerState;
            actionManager.Init(playerBody);
            this.atkManager = atkManager;
            //uIFollowPlayer.Init(playerBody);
            Listen();

            readerMan = InputRead.GetID();

            BattleController.Instance.PollHandler += () =>
            {
                NetDataWriter msg = new NetDataWriter("uppos");
                msg.Put(transform.position.x);
                msg.Put(transform.position.y);
                msg.Put(transform.position.z);
                BattleController.Instance.BattleClient.ClientSend(msg);
            };
        }


        private void Update()
        {
            atkManager?.Update();
            if (!isMy)
            {
                return;
            }
            if (readerMan.HorizontalAxis != 0 || readerMan.VerticalAxis != 0)
            {
                ReadyRun();
            }
            else
            {
                ReadyIdle();
            }
            JundeAtkWay();
        }

        void ReadyRun()
        {
            if (actionManager.NowState == PlayerStateEnum.Run)
            {
                return;            }
            if (PlayerState.BeBacking ||  PlayerState.Dead || PlayerState.Roll)
            {
                return;
            }
            actionManager.ChangeState(PlayerStateEnum.Run);
        }

        void ReadyIdle()
        {
            if (actionManager.NowState == PlayerStateEnum.Idle)
            {
                return;
            }
            if (PlayerState.Idle || PlayerState.BeBacking || PlayerState.Dead || PlayerState.Roll || PlayerState.Move || PlayerState.Walk)
            {
                return;
            }
            actionManager.ChangeState(PlayerStateEnum.Idle);
        }

        #region 攻击动作判断
        /// <summary>
        /// 攻击判断
        /// </summary>
        void JundeAtkWay()
        {
            JudgeAtkWay(Configs.Action.AttackNormal, AtkWayEnmu.Normal);
            JudgeAtkWay(Configs.Action.AttackSpecial, AtkWayEnmu.Special);
            JudgeAtkWay(Configs.Action.AttackSkill1, AtkWayEnmu.Skill_1);
            JudgeAtkWay(Configs.Action.AttackSkill2, AtkWayEnmu.Skill_2);
            JudgeAtkWay(Configs.Action.AttackSkill3, AtkWayEnmu.Skill_3);
            JudgeAtkWay(Configs.Action.AttackSkill4, AtkWayEnmu.Skill_4);
        }

        void JudgeAtkWay(Configs.Action inputAction, AtkWayEnmu atkWay)
        {
            if (readerMan.AttackStartIns(inputAction))
            {
                ReadyAttack(atkWay);
                
            }
            if (readerMan.AttackEndIns(inputAction))
            {
                atkManager.AttackEnd(atkWay);
                BattleController.Instance.SendAktEnd((int)atkWay);
            }
        }
        
        void ReadyAttack(AtkWayEnmu atkWayE)
        {
            if (PlayerState.Attack || PlayerState.BeBacking || PlayerState.Dead || PlayerState.Roll)
            {
                return;
            }
            atkManager.AttackStart(atkWayE);
            
            BattleController.Instance.SendAktStart((int)atkWayE);
        }
        #endregion




        #region 受伤
        /// <summary>
        /// 接收伤害，播放动画，打断攻击动作
        /// </summary>
        /// <param name="dmg"></param>
        /// <param name="force"></param>
        /// <param name="position"></param>
        public override void TakeDamage(float dmg, float force, float forceTime, Vector3 position,int Id=-1)
        {
            if (PlayerState.BeBacking || PlayerState.BeAttackInvincible || PlayerState.Invincible || PlayerState.Roll || PlayerState.Injured||PlayerState.Dead)
            {
                return;
            }
            //发送扣血消息
            BattleController.Instance.SendChangeHp(dmg);

            //playerBody.TakeDamage(dmg, force, position);
            
            /*if (playerBody.NowHp<=0)
            {
                //TODO 死亡动作
                EventManager.Broadcast(EventName.Player.Die, Id);
                playerBody.PlayerState.Dead = true;
                playerBody.animator.SetTrigger("dead");
            }
            else
            {
                BeAtk(dmg, force,forceTime, position);
            }*/
        }
        private void BeAtk(float dmg,float f,float ft,Vector3 origin)
        {
            InterruptAttack(playerBody.Id);

        }

        private Vector3 GetOffSetDir(Vector3 positonDir)
        {
            positonDir.y = 0;
            var v3 = transform.position;
            v3.y = 0;
            Vector3 backTo = v3 - positonDir;
            return backTo.normalized;
        }

        #endregion


        //打断攻击
        private void InterruptAttack(params object[] o)
        {
            if ((int)o[0]==playerBody.Id)
            {
                atkManager.AttackInterrupt();
            }
        }

        private void Listen()
        {
            EventManager.Listen(EventName.Case.PassWave, ChooesReward);
        }

        #region 奖励
        GameObject inCaseRewardPlne;
        private void ChooesReward(object[] o)
        {
            if ((bool)o[1])
            {
                return;
            }
            new Timer(1, false, () =>
            {

                if (inCaseRewardPlne == null)
                {
                    inCaseRewardPlne = Resources.Load<GameObject>(ResourcePath.RewardChooes);
                }
                var clo = Instantiate(inCaseRewardPlne, null);
                //clo.GetComponent<CaseRewardChoose>().Init(playerBody, InCaseRewardPool.GetRandom(3));

            }).Start();
        }

        #endregion


        private void OnDestroy()
        {
            EventManager.UnListen(EventName.Case.PassWave, ChooesReward);
        }
    }
}
